package softtech.apps.autoanswerphonecall;

import java.util.List;
import softtech.apps.autoanswerphonecall.R;
import android.R.color;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class messageAdapter extends BaseAdapter {
	
	private final Context context;
	private final List<Message> lstMessage;
	
	DatabaseHandler db;
	ImageView imvTick;
	
	// Static var to hold message is choose
	public messageAdapter(Context ctx,List<Message> lstMessage) {
		super();
		this.context = ctx;
		this.lstMessage = lstMessage;
		db = new DatabaseHandler(ctx);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(lstMessage.equals(null)||lstMessage == null){
			return 0;
		}
		
		return lstMessage.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position,View convertView, ViewGroup parent) {
		
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
		        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		convertView = inflater.inflate(R.layout.message_manager, parent, false);
		imvTick = (ImageView) convertView.findViewById(R.id.imgTick);
		
		if(lstMessage.get(position).get_is_choose() == 1){
			imvTick.setVisibility(View.VISIBLE);
			imvTick.setImageResource(R.drawable.tick);
		}else{
			imvTick.setVisibility(View.GONE);
		}
		
	    final TextView tvMessageExcerpt = (TextView) convertView.findViewById(R.id.tvMessageExcerpt);
	    String message = lstMessage.get(position).get_message();
	    if(message.length() > 20 ){
	    	tvMessageExcerpt.setText(message.substring(0, 20)+" ...");
	    }else{
	    	tvMessageExcerpt.setText(message);
	    }
	    
	    

	    
	    // Button edit click
	    
	    ImageView imvEdit = (ImageView) convertView.findViewById(R.id.btnEdit);
	    imvEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(context, "Edit button click",Toast.LENGTH_SHORT).show();
				 Intent toDetailActivity = new Intent(context, MessageDetailActivity.class);
//				 Log.d("MES", "message = "+lstMessage.get(position).get_message());
//				 Log.d("ID", "message = "+lstMessage.get(position).get_id());
				 toDetailActivity.putExtra("message_id",lstMessage.get(position).get_id());
				 toDetailActivity.putExtra("message_content",lstMessage.get(position).get_message());
				 toDetailActivity.putExtra("message_is_choose",lstMessage.get(position).get_is_choose());
				 toDetailActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				 context.startActivity(toDetailActivity);
				 
			}
		});
	    
	    
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    final DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
	        @Override
	        public void onClick(DialogInterface dialog, int which) {
	            switch (which){
	            case DialogInterface.BUTTON_POSITIVE:
	                //Yes button clicked
	            	db.deleteMessage(lstMessage.get(position));
					lstMessage.remove(position);
					notifyDataSetChanged();
	                break;

	            case DialogInterface.BUTTON_NEGATIVE:
	                //No button clicked
	                break;
	            }
	        }
	    };
	    
	    // Button delete click
	    ImageView imvDel = (ImageView) convertView.findViewById(R.id.btnDel);
	    imvDel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(context, "Delete button click",Toast.LENGTH_SHORT).show();
				 builder.setMessage("Are you sure?").setNegativeButton("No", dialogClickListener)
			     .setPositiveButton("Yes", dialogClickListener)
			        .show();
				
			}
		});
	    
		return convertView;
	}

}
